package com.example.converter.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class StringToDateConverter implements Converter<String, Date> {
    public static final Logger logger = LoggerFactory.getLogger(StringToDateConverter.class);
    static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 格式转换的还需要
     * @param source
     * @return
     */
    @Override
    public Date convert(String source) {
        Date date = new Date();
        try {
            date =  simpleDateFormat.parse(source);
        } catch (ParseException e) {
            logger.info(source + "转换异常");
            e.printStackTrace();
        }
        return date;
    }
}
